<template>
  <div id="login">
    <div class="login-container">
      <h3 class="title">承运人登录</h3>
      <div class="toggle fb">
        <span @click="toggle = false" :class="!toggle ? 'active' : ''"
          >密码登录</span
        >
        <span @click="toggle = true" :class="toggle ? 'active' : ''"
          >短信登录</span
        >
      </div>
      <!-- 密码登录 -->
      <el-form
        v-if="!toggle"
        label-position="left"
        :model="ruleForm"
        :rules="rules"
      >
        <el-form-item prop="phone" key="1">
          <el-input
            prefix-icon="el-icon-mobile-phone"
            type="text"
            v-model="ruleForm.phone"
            placeholder="请输入手机号"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password" key="2">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            v-model="ruleForm.password"
            placeholder="请输入密码"
          >
          </el-input>
        </el-form-item>
        <slideVerify key="7" />
        <el-form-item style="width:100%;" key="3">
          <el-button type="primary" style="width:100%;" @click="loginTo()"
            >加入</el-button
          >
        </el-form-item>
        <div style="width:100%;margin-top: -10px;" class="fb">
          <el-checkbox class="remember" v-model="rememberpwd"
            >记住密码
          </el-checkbox>
          <div>
            <router-link tag="span" to="" class="regist">忘记密码</router-link
            >&emsp;
            <router-link tag="span" :to="{ path: '/register' }" class="regist"
              >立即注册</router-link
            >
          </div>
        </div>
      </el-form>
      <!-- 手机号登录 -->
      <el-form
        v-if="toggle"
        label-position="left"
        :model="ruleForm"
        :rules="rules"
      >
        <el-form-item prop="phone2" key="4">
          <el-input
            prefix-icon="el-icon-mobile-phone"
            type="text"
            v-model="ruleForm.phone2"
            placeholder="请输入手机号"
          >
          </el-input>
        </el-form-item>
        <slideVerify key="8" />
        <el-form-item prop="code" key="5">
          <el-input
            prefix-icon="el-icon-message"
            placeholder="短信验证码"
            v-model="ruleForm.code"
          >
            <template slot="append">
              <button
                @click="resetTime()"
                style="background: none;line-height: 58px;cursor: pointer;color: #56aaff;"
                :disabled="disabled"
              >
                {{ btnText }}
              </button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item style="width:100%;" key="6">
          <el-button type="primary" style="width:100%;">加入</el-button>
        </el-form-item>

        <div style="width:100%;margin-top: -10px;" class="fb">
          <el-checkbox class="remember" style="visibility:hidden">
          </el-checkbox>
          <div>
            <router-link tag="span" to="" class="regist">忘记密码</router-link
            >&emsp;
            <router-link tag="span" :to="{ path: '/register' }" class="regist"
              >立即注册</router-link
            >
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import store from "@/store/index";
import { apiAddress } from "@/utils/api";
import { setCookie, getCookie } from "../utils/util";
import slideVerify from "@/components/slideVerify.vue";
export default {
  name: "login",
  components: {
    slideVerify
  },
  data() {
    return {
      toggle: false,
      disabled: false,
      btnText: "发送验证码",
      rememberpwd: false,
      ruleForm: {
        //phone和password默认为空
        phone: "",
        password: "",
        phone2: "",
        code: ""
      },
      //rules前端验证
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^(1[3-9])\d{9}$/,
            message: "输入的手机号不合法",
            trigger: "blur"
          }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        phone2: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^(1[3-9])\d{9}$/,
            message: "输入的手机号不合法",
            trigger: "blur"
          }
        ],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      }
    };
  },
  created() {
    console.log('测试' + store.state.token);
    // 在页面加载时从cookie获取登录信息
    let phone = getCookie("phone");
    let password = getCookie("password");
    // 如果存在赋值给表单，并且将记住密码勾选
    if (this.rememberpwd) {
      this.ruleForm.phone = phone;
      this.ruleForm.password = password;
      this.rememberpwd = true;
    }
  },
  mounted() {},
  methods: {
    // 登录
    loginTo() {
      apiAddress({
        phone: '17639806692'
      }).then(res => {
        // 获取数据成功后的其他操作
        console.log(res);
      });
      this.$router.push('/index')
      this.setUserInfo();
    },
    // 储存表单信息
    setUserInfo() {
      // 判断用户是否勾选记住密码，如果勾选，向cookie中储存登录信息，
      // 如果没有勾选，储存的信息为空
      console.log(this.rememberpwd);
      if (this.rememberpwd) {
        setCookie("phone", this.phone, 30);
        setCookie("password", this.password, 30);
      } else {
        setCookie("phone", "");
        setCookie("password", "");
      }
    },
    // 倒计时
    resetTime() {
      var second = 60;
      var timer = null;
      timer = setInterval(() => {
        second -= 1;
        if (second > 0) {
          this.disabled = true;
          this.btnText = second + "S";
        } else {
          clearInterval(timer);
          this.disabled = false;
          this.btnText = "重新获取";
        }
      }, 1000);
    }
  }
};
</script>
<style lang="scss">
#login {
  .el-input__inner {
    line-height: 60px;
    height: 60px;
  }
  .el-form-item {
    margin-bottom: 30px;
  }
  .el-button {
    height: 60px;
    font-size: 20px;
    border-radius: 10px;
    margin-top: 30px;
  }
  .el-input-group--append {
    margin-top: 30px;
  }
  .el-input-group__append {
    width: 68px;
    text-align: center;
    cursor: pointer;
    background-color: #fff;
    color: #56aaff;
  }
}
</style>

<style lang="scss" scoped>
// @keyframes imgToggle {
//   0% {
//     background-image: url("../assets/img/homebg1.jpg");
//   }

//   100% {
//     background-image: url("../assets/img/homebg2.jpg");
//   }
// }

// .imgToggle {
//   animation-duration: 5s;
//   -webkit-animation-name: mslidedown;
//   animation-name: mslidedown;
//   animation-timing-function: cubic-bezier(1, 0.1, 0.4, 1);
// }
#login {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  background-image: url("../assets/img/homebg2.jpg");
  background-repeat: no-repeat;
  background-position: center center;
  // background-size: 100%;
  position: relative;
  .login-container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    width: 650px;
    padding: 70px 125px 55px;
    background: #fff;
    box-sizing: border-box;
    // border: 1px solid #eaeaea;
    text-align: left;
    box-shadow: 0px 1px 25px 0px rgba(192, 192, 192, 0.75);
    .title {
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: rgba(0, 0, 0, 1);
      line-height: 20px;
      opacity: 0.9;
      text-align: center;
    }
    .toggle {
      border-bottom: 1px solid #dcdfe6;
      margin-bottom: 30px;
      margin-top: 30px;
      span {
        cursor: pointer;
        line-height: 34px;
        color: rgba(0, 0, 0, 0.6);
      }
      .active {
        border-bottom: 1px solid #56aaff;
        color: rgba(0, 0, 0, 1);
        box-sizing: border-box;
        line-height: 33px;
      }
    }
    .regist {
      cursor: pointer;
      // color: #56aaff;
      font-size: 14px;
      &:hover {
        color: #56aaff;
      }
    }
  }
}
</style>
